/**
 * 
 */
package com.star.encryption;

import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * @ClassName: EncryptClasses
 * @Description: TODO
 * @author starhq
 * @createtime 2014年11月29日 下午3:10:09
 * @email : jian.wang@shinsoft.net
 * @modify by TODO
 * @resaon
 * @edittime
 * @version 1.0
 * @copyright shinsoft
 */
public class EncryptClasses {

	// 加密指定目录下的所有class文件
	// public static void encrypt(File folder) throws IOException,
	// InvalidKeyException, NoSuchAlgorithmException,
	// InvalidKeySpecException, NoSuchPaddingException,
	// IllegalBlockSizeException, BadPaddingException {
	// String keyFilename = "key.data";
	// String algorithm = "DES";
	//
	// // 生成密匙
	// SecureRandom sr = new SecureRandom();
	// byte rawKey[] = KeyUtil.readKey(keyFilename);
	// DESKeySpec dks = new DESKeySpec(rawKey);
	// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
	// SecretKey key = keyFactory.generateSecret(dks);
	//
	// // 创建用于实际加密操作的Cipher对象
	// Cipher ecipher = Cipher.getInstance(algorithm);
	// ecipher.init(Cipher.ENCRYPT_MODE, key, sr);
	//
	// for (int i = 0; i < files.size(); i++) {
	// String filename = files.get(i).getName();
	// byte classData[] = KeyUtil.readKey(filename); // 读入类文件
	// byte encryptedClassData[] = ecipher.doFinal(classData); // 加密
	// KeyUtil.writeKey(filename, encryptedClassData); // 保存加密后的内容
	// System.out.println("Encrypted " + filename);
	// }
	//
	// for (final File fileEntry : folder.listFiles()) {
	// if (fileEntry.isDirectory()) {
	// listFilesForFolder(fileEntry);
	// } else {
	// String fileName = fileEntry.getAbsolutePath();
	// names.add(fileName);
	// }
	// }
	// }

	public static void encrypt(final File folder) throws IOException,
			InvalidKeyException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			IllegalBlockSizeException, BadPaddingException {
		String keyFilename = "key.data";
		String algorithm = "DES";

		// 生成密匙
		SecureRandom sr = new SecureRandom();
		byte rawKey[] = KeyUtil.readKey(keyFilename);
		DESKeySpec dks = new DESKeySpec(rawKey);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
		SecretKey key = keyFactory.generateSecret(dks);

		// 创建用于实际加密操作的Cipher对象
		Cipher ecipher = Cipher.getInstance(algorithm);
		ecipher.init(Cipher.ENCRYPT_MODE, key, sr);
		for (final File fileEntry : folder.listFiles()) {
			if (fileEntry.isDirectory()) {
				encrypt(fileEntry);
			} else {
				String fileName = fileEntry.getAbsolutePath();
				byte classData[] = KeyUtil.readKey(fileName); // 读入类文件
				byte encryptedClassData[] = ecipher.doFinal(classData); // 加密
				KeyUtil.writeKey(fileName, encryptedClassData); // 保存加密后的内容
			}
		}
	}

	static public void main(String args[]) throws Exception {

		encrypt(new File("d://bin"));
		// String keyFilename = "key.data";
		// String algorithm = "DES";
		//
		// // 生成密匙
		// SecureRandom sr = new SecureRandom();
		// byte rawKey[] = KeyUtil.readKey(keyFilename);
		// DESKeySpec dks = new DESKeySpec(rawKey);
		// SecretKeyFactory keyFactory =
		// SecretKeyFactory.getInstance(algorithm);
		// SecretKey key = keyFactory.generateSecret(dks);
		//
		// // 创建用于实际加密操作的Cipher对象
		// Cipher ecipher = Cipher.getInstance(algorithm);
		// ecipher.init(Cipher.ENCRYPT_MODE, key, sr);
		//
		// // 加密命令行中指定的每一个类
		// for (int i = 1; i < args.length; ++i) {
		// String filename = args[i];
		// byte classData[] = KeyUtil.readKey(filename); // 读入类文件
		// byte encryptedClassData[] = ecipher.doFinal(classData); // 加密
		// KeyUtil.writeKey(filename, encryptedClassData); // 保存加密后的内容
		// System.out.println("Encrypted " + filename);
		// }
	}
}
